package com.guoning.office_system.controller;

import com.guoning.office_system.model.User;
import com.guoning.office_system.service.FunctionService;
import com.guoning.office_system.utils.Constants;
import com.guoning.office_system.utils.JsonResult;
import com.guoning.office_system.utils.TokenProccessor;
import com.guoning.office_system.utils.TokenTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("menu")
public class MenuController {
    @Autowired
    private FunctionService functionService;

    /**
     * 获取左侧菜单
     * @param request
     * @param response
     * @param token
     * @return
     */
    @RequestMapping(value = "/getUserAllMenuList")
    public JsonResult getUserAllMenuList(HttpServletRequest request, HttpServletResponse response, String token){
        JsonResult js;
        try {
            User user = (User) request.getSession().getAttribute(request.getHeader("Token"));
            List<Map> list = functionService.getMenuList(user.getId());
            if (list.size() == 0) {
                js = new JsonResult(Constants.STATUS_FAIL, "获取左侧菜单失败");
            }else {
                js = new JsonResult(Constants.STATUS_SUCCESS, "获取左侧菜单成功", list);
            }
        }catch (Exception ex){
            ex.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "获取左侧菜单异常");
        }
        return js;
    }
}
